package com.sentrix.auto.printercenter.infrastructure.config;

import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.logicdelete.AbstractLogicDeleteProcessor;
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
import com.mybatisflex.core.logicdelete.impl.PrimaryKeyLogicDeleteProcessor;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;

@Slf4j
@MapperScan("com.sentrix.auto.printercenter.infrastructure.persistence.dao.mapper")
@Configuration
public class MyDBFlexConfig implements MyBatisFlexCustomizer {

    @Override
    public void customize(FlexGlobalConfig flexGlobalConfig) {
        log.info("flex 个性化配置......");
        FlexGlobalConfig.KeyConfig keyConfig = flexGlobalConfig.getKeyConfig();
        if (Objects.isNull(keyConfig)) {
//            keyConfig = new FlexGlobalConfig.KeyConfig();
//            flexGlobalConfig.setKeyConfig(keyConfig);
//
//            keyConfig.setKeyType(KeyType.Generator);
//            keyConfig.setValue(KeyGenerators.snowFlakeId);
//            keyConfig.setBefore(Boolean.TRUE);
        }

        flexGlobalConfig.setNormalValueOfLogicDelete(0);
        flexGlobalConfig.setLogicDeleteColumn("delete_id");
        LogicDeleteManager.setProcessor(new MyLogicDeleteProcessor());


        /**
         * 开票sql审计功能
         */
        //开启审计功能
        AuditManager.setAuditEnable(true);
        AuditManager.setMessageCollector(auditMessage ->
                log.info("{},{}ms", auditMessage.getFullSql()
                        , auditMessage.getElapsedTime())
        );
    }


    public static class MyLogicDeleteProcessor extends AbstractLogicDeleteProcessor {

        @Override
        public Object getLogicNormalValue() {
            return FlexGlobalConfig.getDefaultConfig().getNormalValueOfLogicDelete();
        }

        @Override
        public Object getLogicDeletedValue() {
            return "id";
        }
    }


}
